package nccloud.web.pu.storereq.action;

import nc.bs.dao.BaseDAO;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.pu.m422x.entity.StoreReqAppHeaderVO;
import nc.vo.pu.m422x.entity.StoreReqAppItemVO;
import nc.vo.pu.m422x.entity.StoreReqAppVO;
import nc.vo.scmpub.res.billtype.POBillType;
import nc.vo.scmpub.util.AppInfoContext;
import nccloud.dto.scmpub.script.entity.SCMScriptResultDTO;
import nccloud.framework.core.json.IJson;
import nccloud.framework.core.util.GridCompareUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.model.row.Cell;
import nccloud.framework.web.ui.model.row.Row;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.framework.web.ui.pattern.billcard.BillCardOperator;
import nccloud.pubitf.riart.pflow.CloudPFlowContext;
import nccloud.pubitf.scmpub.commit.service.IBatchRunScriptService;
import nccloud.web.pu.storereq.entity.QueryInfoStorereq;
import nccloud.web.pu.storereq.util.StoreReqScaleUtil;
import nccloud.web.scmpub.pub.resexp.PfResumeExceptionNccUtils;

public class StorereqInfoNewSaveAction implements ICommonAction {
	public Object doAction(IRequest request) {
		BillCard card = null;
		BillCard oldCard = null;
		BillCard returnCard = null;
		String read = request.read();
		IJson json = JsonFactory.create();
		QueryInfoStorereq query = (QueryInfoStorereq) json.fromJson(read, QueryInfoStorereq.class);
		String type = query.getType();
		PfResumeExceptionNccUtils.before(request);
		BillCardOperator operator = new BillCardOperator();
		StoreReqAppVO[] vos = null;

		try {
			StoreReqAppVO vo = (StoreReqAppVO) operator.toBill(request);
			oldCard = operator.toCard(vo);
			this.afterProcess(oldCard);
			vo.getParent().setStatus(2);
			vos = new StoreReqAppVO[]{vo};
			CloudPFlowContext context = new CloudPFlowContext();
			context.setBillType(POBillType.MRBill.getCode());
			context.setBillVos(vos);
			context.setActionName("SAVEBASE");
			AppInfoContext.setBtnCode("Save");
			SCMScriptResultDTO obj = ((IBatchRunScriptService) ServiceLocator.find(IBatchRunScriptService.class))
					.runBacth(context, StoreReqAppVO.class);
			card = operator.toCard(obj.getSucessVOs()[0]);
			this.afterProcess(card);
			if (type != null) {
				return card;
			}
            //wxp begin 
			returnCard = GridCompareUtils.compareBillCardGrid(operator.getOriginBillcard(), card);
			
			
			
			Row[] rows =returnCard.getBody().getModel().getRows();
			for(Row row:rows) {
			 String sqlpq =(String) row.getCell("pk_storereq").getValue();
			 String sqls ="select vbillcode from po_storereq where pk_storereq='"+sqlpq+"'";
			 
			 String messages = (String) new BaseDAO().executeQuery(sqls, new ColumnProcessor() );
			 String messagesql= "select pk_defdoc from bd_defdoc where code ='"+messages+"'";
			 String message = (String) new BaseDAO().executeQuery(messagesql, new ColumnProcessor() );
			 
//			 String sqld="update po_storereq_b set vfree1='"+message+"' where pk_storereq='"+sqlpq+"' ";
//			 
//			 new BaseDAO().executeUpdate(sqld);
//			 
//			 
//			 Cell cell =new Cell();
//				
//				    cell.setScale(-1);
//		
//					cell.setDisplay(messages);
//					cell.setValue(message);
//					
//					
//					row.addCell("vfree1",cell);			
					
			}
	    //end
			
		} catch (Exception var14) {
			PfResumeExceptionNccUtils.handleResumeException(var14);
		}

		return returnCard;
	}

	public void afterProcess(BillCard card) {
		StoreReqScaleUtil stoUtil = new StoreReqScaleUtil();
		stoUtil.processBillCard(card);
	}
}